package com.xp.kettlegenerate.service.impl;

import com.alibaba.druid.Constants;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xp.kettlegenerate.entity.DataBaseMeta;
import com.xp.kettlegenerate.entity.GenerateTable;
import com.xp.kettlegenerate.enu.DataBaseDriverEnum;
import com.xp.kettlegenerate.mapper.GenerateTableMapper;
import com.xp.kettlegenerate.service.IDataBaseMetaService;
import com.xp.kettlegenerate.service.IGenerateTableService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xp.kettlegenerate.start.Generate;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateUtils;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleXMLException;
import org.pentaho.di.core.plugins.PluginRegistry;
import org.pentaho.di.trans.TransMeta;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xxx
 * @since 2024-06-17
 */
@Service
@Slf4j
public class GenerateTableServiceImpl extends ServiceImpl<GenerateTableMapper, GenerateTable> implements IGenerateTableService {

    @Autowired
    IDataBaseMetaService  dataBaseMetaService;

    /**
     * 生成转换
     *
     * @return
     */
    @Override
    public TransMeta generateTrans() throws KettleException {


        TransMeta transMeta = new TransMeta();
        DatabaseMeta sourceDataBase = dataBaseMetaService.getDatabaseMeat(0);
        DatabaseMeta targetDataBase = dataBaseMetaService.getDatabaseMeat(1);

        DataBaseMeta dataBaseMeta = dataBaseMetaService.getOne(new LambdaQueryWrapper<DataBaseMeta>().eq(DataBaseMeta::getDatabaseType, 1));

        String targetSchema = dataBaseMeta.getSchame();
        //获取schema
        if(DataBaseDriverEnum.ORACLE.dbType.equals(dataBaseMeta.getDbType())){
            targetSchema = dataBaseMeta.getUserName();
        }

        transMeta.addDatabase(0,sourceDataBase);
        transMeta.addDatabase(1,targetDataBase);
        String tranMetaName = "全量采集表输入和表输出" + new Date().getTime();
        transMeta.setName(tranMetaName);

        List<GenerateTable> list = list();

        PluginRegistry registry = PluginRegistry.getInstance();
        AtomicInteger locationX = new AtomicInteger(100);
        AtomicInteger locationY = new AtomicInteger(100);

        for (GenerateTable generateTable : list) {
            String sql = " select * from "+generateTable.getLeftQuo()+generateTable.getSourceName()+generateTable.getRightQuo();
            String inputName = "表-"+generateTable.getSourceName()+  "-输入";
            String outputName = generateTable.getLeftQuo()+generateTable.getTargetName()+generateTable.getRightQuo();
            Generate.generateHalf(transMeta,registry,locationX,locationY,targetDataBase,sql,inputName,outputName,targetSchema);
            locationX.getAndSet(100);
            locationY.addAndGet(100);
        }

        String xml = transMeta.getXML();

        Generate.saveMetaString(xml,tranMetaName+".ktr");
        log.info("生成成功:{}",tranMetaName);
        return transMeta;
    }
}
